package com.zhentao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhentao.pojo.UserCoupon;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户优惠券Mapper接口
 */
@Mapper
public interface UserCouponMapper extends BaseMapper<UserCoupon> {
    
    /**
     * 查询用户可用的优惠券
     */
    @Select("SELECT uc.*, c.* FROM user_coupon uc " +
            "LEFT JOIN coupon c ON uc.coupon_id = c.id " +
            "WHERE uc.user_id = #{userId} AND uc.status = 1 " +
            "AND c.valid_start_time <= NOW() AND c.valid_end_time >= NOW() " +
            "AND c.is_enabled = 1 AND c.remaining_stock > 0")
    List<UserCoupon> selectUserAvailableCoupons(@Param("userId") Long userId);
    
    /**
     * 查询用户所有优惠券
     */
    @Select("SELECT uc.*, c.* FROM user_coupon uc " +
            "LEFT JOIN coupon c ON uc.coupon_id = c.id " +
            "WHERE uc.user_id = #{userId} " +
            "ORDER BY uc.receive_time DESC")
    List<UserCoupon> selectUserAllCoupons(@Param("userId") Long userId);
} 